package com.lvy.hczn.front.business.controller;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lvy.hczn.front.common.annotation.Log;
import com.lvy.hczn.front.common.constant.RedisConstants;
import com.lvy.hczn.front.common.core.domain.AjaxResult;
import com.lvy.hczn.front.common.core.domain.BaseModel;
import com.lvy.hczn.front.common.core.redis.RedisCache;
import com.lvy.hczn.front.common.enums.BusinessType;
import com.lvy.hczn.front.common.exception.UtilException;
import com.lvy.hczn.front.common.utils.poi.ExcelUtil;
import com.lvy.hczn.front.system.domain.system.SysArea;
import com.lvy.hczn.front.system.service.ISysAreaService;
import com.lvy.hczn.front.system.service.WarehouseService;
import com.lvy.hczn.front.common.core.controller.BaseController;
import com.lvy.hczn.front.common.core.page.TableDataInfo;
import com.lvy.hczn.front.system.domain.business.Warehouse;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/system/warehouse")
@Slf4j
public class WarehouseController extends BaseController {

    private String prefix = "system/warehouse";

    @Autowired
    private WarehouseService warehouseService;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private ISysAreaService sysAreaService;

    @RequiresPermissions("system:warehouse:view")
    @GetMapping()
    public String warehouse() {
        return prefix + "/warehouse";
    }

    /**
     * 分页列表查询
     *
     * @param warehouse
     * @return
     */
    @RequiresPermissions("system:warehouse:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Warehouse warehouse) {
        startPage();
        List<Warehouse> list = warehouseService.getPageList(warehouse);
        return getDataTable(list);
    }

    /**
     * 导出仓库信息列表
     */
    @RequiresPermissions("system:warehouse:export")
    @Log(title = "仓库信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Warehouse warehouse) {
        List<Warehouse> list = warehouseService.getPageList(warehouse);
        ExcelUtil<Warehouse> util = new ExcelUtil<Warehouse>(Warehouse.class);
        return util.exportExcel(list, "仓库信息数据");
    }

    /**
     * 新增仓库信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        List<Warehouse> list = redisCache.getCacheList(RedisConstants.WAREHOUSE_PARENT_LIST);
        mmap.put("list", list);
        return prefix + "/add";
    }


    /**
     * 仓库新增
     *
     * @param warehouse
     * @return
     */
    @PostMapping("/add")
    @RequiresPermissions("system:warehouse:add")
    @ResponseBody
    @Log(title = "仓库信息", businessType = BusinessType.INSERT)
    public AjaxResult add(Warehouse warehouse) {
        try {
            return AjaxResult.success(warehouseService.saveData(warehouse, true));
        } catch (Exception e) {
            if (e instanceof UtilException) {
                return AjaxResult.error(e.getMessage());
            } else {
                log.error("generateWarehouseCode同步异常：{}", e);
                return AjaxResult.error("系统异常");
            }
        }
    }

    /**
     * 修改仓库信息
     */
    @RequiresPermissions("system:warehouse:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        Warehouse warehouse = warehouseService.getById(id);
        List<Warehouse> list = redisCache.getCacheList(RedisConstants.WAREHOUSE_PARENT_LIST);
        mmap.put("list", list);
        mmap.put("warehouse", warehouse);
        return prefix + "/edit";
    }

    /**
     * 修改保存仓库信息
     */
    @RequiresPermissions("system:warehouse:edit")
    @Log(title = "仓库信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Warehouse warehouse) {
        try {
            return toAjax(warehouseService.updateData(warehouse));
        } catch (Exception e) {
            if (e instanceof UtilException) {
                return AjaxResult.error(e.getMessage());
            } else {
                log.error("editSave编辑异常：{}", e);
                return AjaxResult.error("系统异常");
            }
        }
    }

    /**
     * 删除仓库信息
     */
    @RequiresPermissions("system:warehouse:remove")
    @Log(title = "仓库信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(warehouseService.removeByIds(ListUtil.toList(ids.split(","))));
    }

    /**
     * 仓库信息初始化
     *
     * @param warehouse
     * @return
     */
    @PostMapping("/initWarehouse")
    @RequiresPermissions("system:warehouse:add")
    @ResponseBody
    @Log(title = "仓库信息初始化", businessType = BusinessType.INSERT)
    public AjaxResult initWarehouse(Warehouse warehouse) {
        try {
            return AjaxResult.success(warehouseService.initWarehouse());
        } catch (Exception e) {
            if (e instanceof UtilException) {
                return AjaxResult.error(e.getMessage());
            } else {
                log.error("generateWarehouseCode同步异常：{}", e);
                return AjaxResult.error("系统异常");
            }
        }
    }
}
